<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="回复时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="回复时间"></el-input>
    </el-form-item>
    <el-form-item label="回帖人id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="回帖人id"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="userAvatar">
      <el-input v-model="dataForm.userAvatar" placeholder="用户头像"></el-input>
    </el-form-item>
    <el-form-item label="回复内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="回复内容"></el-input>
    </el-form-item>
    <el-form-item label="回复已读未读" prop="state">
      <el-input v-model="dataForm.state" placeholder="回复已读未读"></el-input>
    </el-form-item>
    <el-form-item label="用户昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="主贴id" prop="articleId">
      <el-input v-model="dataForm.articleId" placeholder="主贴id"></el-input>
    </el-form-item>
    <el-form-item label="" prop="likeNum">
      <el-input v-model="dataForm.likeNum" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createtime: '',
          userId: '',
          userAvatar: '',
          content: '',
          state: '',
          nickname: '',
          articleId: '',
          likeNum: ''
        },
        dataRule: {
          createtime: [
            { required: true, message: '回复时间不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '回帖人id不能为空', trigger: 'blur' }
          ],
          userAvatar: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '回复内容不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '回复已读未读不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          articleId: [
            { required: true, message: '主贴id不能为空', trigger: 'blur' }
          ],
          likeNum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/main/reply/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createtime = data.reply.createtime
                this.dataForm.userId = data.reply.userId
                this.dataForm.userAvatar = data.reply.userAvatar
                this.dataForm.content = data.reply.content
                this.dataForm.state = data.reply.state
                this.dataForm.nickname = data.reply.nickname
                this.dataForm.articleId = data.reply.articleId
                this.dataForm.likeNum = data.reply.likeNum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/main/reply/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createtime': this.dataForm.createtime,
                'userId': this.dataForm.userId,
                'userAvatar': this.dataForm.userAvatar,
                'content': this.dataForm.content,
                'state': this.dataForm.state,
                'nickname': this.dataForm.nickname,
                'articleId': this.dataForm.articleId,
                'likeNum': this.dataForm.likeNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
